import { supabase } from "./supabase";

// 项目类型定义
export interface Project {
    project_id: string;
    team_id: string;
    user_id: string;
    name: string;
    content: string;
    search_keywords: string[] | null;
    matching_rules: string[] | null;
    search_conditions?: any;
    search_objective: 'posts' | 'creator';
    remark?: string;
    status: 'ACTIVE' | 'DELETE';
    create_time: string;
    update_time: string;
}

/**
 * 创建新项目
 * @param projectData 项目数据
 * @returns 创建的项目信息
 */
interface ProjectData {
    name: string;
    content: string;
    team_id: string;
    search_objective: 'posts' | 'creator';
    search_conditions: any;
}
export const createProject = async (projectData: ProjectData, user_id: string) => {
    const now = new Date().toISOString();
    // 创建项目记录
    const data = await supabase
        .from('projects')
        .insert([
            {
                ...projectData,
                user_id,
                status: 'ACTIVE',
                create_time: now,
                update_time: now,
                search_keywords: null,
                matching_rules: null,
            }
        ])
        .select()
        .single();
    return data
};

/**
 * 获取项目详情
 * @param projectId 项目ID
 * @returns 项目信息
 */
export const getProject = async (projectId: string) => {
    const data: { data: Project | null, error: Error | null } = await supabase
        .from('projects')
        .select('*')
        .eq('project_id', projectId)
        .eq('status', 'ACTIVE')
        .single();

    return data;
};

export const getProjectsByTeamId = async (teamId: string) => {
    const data: { data: Project[] | null, error: Error | null } = await supabase
        .from('projects')
        .select('*')
        .eq('team_id', teamId)
        .eq('status', 'ACTIVE');
    return data;
}

export const deleteProject = async (projectId: string) => {
    const now = new Date().toISOString();
    const data: { data: Project | null, error: Error | null } = await supabase
        .from('projects')
        .update({
            status: 'DELETE',
            update_time: now,
        })
        .eq('project_id', projectId)
        .select()
        .single();
    return data;
}


export const updateProject = async (projectId: string, projectData: { matching_rules: string[] | null }) => {
    const now = new Date().toISOString();
    const data: { data: Project | null, error: Error | null } = await supabase
        .from('projects')
        .update({
            ...projectData,
            update_time: now,
        })
        .eq('project_id', projectId)
        .select()
        .single();
    return data;
}

export interface Task {
    task_id: string;
    project_id: string;
    task_type: string;
    input_data: any;
    output_data: any;
    status: string;
    create_time: string;
    update_time: string;
}

export const getTasksByProjectId = async (projectId: string) => {
    const data: { data: Task | null, error: Error | null } = await supabase
        .from('tasks')
        .select('*')
        .eq('project_id', projectId)
        .eq('task_type', 'content_search')
        .single();
    return data;
}


export interface ResultType {
    result_id: string;
    project_id: string;
    creator_id: string;
    post_id: string;
    is_deleted: boolean;
    search_result: any;
    match_score: any;
    user_verdict: string;
    create_time: string;
    update_time: string;
}

export const getResultsByProjectId = async (projectId: string) => {
    const data: { data: ResultType[] | null, error: Error | null } = await supabase
        .from('results')
        .select('*')
        .eq('project_id', projectId)
        .order('create_time', { ascending: false });
    return data;
}


export const updateResult = async (resultId: string, resultData: { user_verdict: string }) => {
    const now = new Date().toISOString();
    const data: { data: ResultType | null, error: Error | null } = await supabase
        .from('results')
        .update({
            ...resultData,
            update_time: now,
        })
        .eq('result_id', resultId)
        .select()
        .single();
    return data;
}